/* 日期格式化 */
export function dateFormat(date: Date, fmt = "yyyy-MM-dd hh:mm:ss") {
    date = new Date(date);
    const o: {[key:string]:any} = {
        "M+": date.getMonth() + 1, //月份
        "d+": date.getDate(), //日
        "h+": date.getHours(), //小时
        "m+": date.getMinutes(), //分
        "s+": date.getSeconds(), //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        S: date.getMilliseconds(), //毫秒
    };
    const yearRegx = /(y+)/;
    if (yearRegx.test(fmt)) {
        const value = yearRegx.exec(fmt)?.[1];
        if (value) {
            fmt = fmt.replace(value, (date.getFullYear() + "").substring(4 - value.length));
        }
    }
    for (let k in o) {
        const regExp = new RegExp("(" + k + ")");
        if (regExp.test(fmt)) {
            const value = regExp.exec(fmt)?.[1];
            if (value) {
                fmt = fmt.replace(value, value.length == 1 ? o[k] : ("00" + o[k]).substring(("" + o[k]).length));
            }
        }
    }
    return fmt;
}

/* Fullscreen */
export function fullScreen(element: any = document.documentElement, doc: any = document) {
    let isFull = !!(doc.webkitIsFullScreen || doc.mozFullScreen || doc.msFullscreenElement || doc.fullscreenElement);
    if (isFull) {
        if (doc.exitFullscreen) {
            doc.exitFullscreen();
        } else if (doc.msExitFullscreen) {
            doc.msExitFullscreen();
        } else if (doc.mozCancelFullScreen) {
            doc.mozCancelFullScreen();
        } else if (doc.webkitExitFullscreen) {
            doc.webkitExitFullscreen();
        }
    } else {
        if (element.requestFullscreen) {
            element.requestFullscreen();
        } else if (element.msRequestFullscreen) {
            element.msRequestFullscreen();
        } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen();
        } else if (element.webkitRequestFullscreen) {
            element.webkitRequestFullscreen();
        }
    }
}

/* 千分符 */
export function groupSeparator(num: any) {
    num = num + "";
    if (!num.includes(".")) {
        num += ".";
    }
    return num
        .replace(/(\d)(?=(\d{3})+\.)/g, function ($0: any, $1: any) {
            return $1 + ",";
        })
        .replace(/\.$/, "");
}

/**
 * 生成随机长度的字符串
 */
export function randomString(length: number) {
    let result = "";
    const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
    const charactersLength = characters.length;
    for (let i = 0; i < length; i++) {
        result += characters.charAt(Math.floor(Math.random() * charactersLength));
    }
    return result;
}


/**
 * 转换文件大小为中文
 * @param value
 */
export function sizeFormat(value: number): string {
    if(value ===0 ) return 0 + "B";
    let i = Math.floor(Math.log(value) / Math.log(1024)),
        sizes = [ 'B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB' ];
    let num: string = (value / Math.pow(1024, i)).toFixed(2);
    return num + ' ' + sizes[i];
}

/**
 * 文件图标
 * @param type
 */
export function fileIcon(type: string) {
    type? type.toLowerCase():'unknown';
    if (type === 'pdf') {
        return new URL(`@/assets/icons/file/pdf.png`, import.meta.url).href
    } else if (type === "gif") {
        return new URL(`@/assets/icons/file/gif.png`, import.meta.url).href
    } else if (type === "jpg") {
        return new URL(`@/assets/icons/file/jpg.png`, import.meta.url).href
    } else if (type === "png" || type === "tiff" || type === "tif" || type === "bmp") {
        return new URL(`@/assets/icons/file/img.png`, import.meta.url).href
    } else if (type === "doc" || type === "docx") {
        return new URL(`@/assets/icons/file/word.png`, import.meta.url).href
    } else if (type === "xls" || type === "xlsx") {
        return new URL(`@/assets/icons/file/xls.png`, import.meta.url).href
    } else if (type === "ppt" || type === "pptx") {
        return new URL(`@/assets/icons/file/ppt.png`, import.meta.url).href
    } else if (type === 'txt') {
        return new URL(`@/assets/icons/file/txt.png`, import.meta.url).href
    } else if (type === 'zip' || type === 'rar') {
        return new URL(`@/assets/icons/file/zip.png`, import.meta.url).href
    } else if (type === 'mp4') {
        return new URL(`@/assets/icons/file/video.png`, import.meta.url).href
    } else if (type === "mp3" || type === "wav" || type === "aac") {
        return new URL(`@/assets/icons/file/music.png`, import.meta.url).href
    } else if (type === "folder") {
        return new URL(`@/assets/icons/file/folder.png`, import.meta.url).href
    } if (type === "folder-open") {
        return new URL(`@/assets/icons/file/folder-open.png`, import.meta.url).href
    } if (type === "folder-question") {
        return new URL(`@/assets/icons/file/folder-question.png`, import.meta.url).href
    } else if (type === "apk") {
        return new URL(`@/assets/icons/file/apk.png`, import.meta.url).href
    } else if (type === "cad") {
        return new URL(`@/assets/icons/file/cad.png`, import.meta.url).href
    } else if (type === "exe") {
        return new URL(`@/assets/icons/file/exe.png`, import.meta.url).href
    } else if (type === "ios") {
        return new URL(`@/assets/icons/file/ipa.png`, import.meta.url).href
    } else if (type === "torrent") {
        return new URL(`@/assets/icons/file/torrent.png`, import.meta.url).href
    } else if (type === "vsd") {
        return new URL(`@/assets/icons/file/vsdT.png`, import.meta.url).href
    } else if (type === "html") {
        return new URL(`@/assets/icons/file/html.png`, import.meta.url).href
    } else {
        return new URL(`@/assets/icons/file/other.png`, import.meta.url).href
    }
}
